package com.neusoft.yuejian12.yuejian_12.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.javabean.UserBean;
import com.neusoft.yuejian12.yuejian_12.model.UserModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegisterActivity extends AppCompatActivity {
    //声明
    private ImageButton imageBack;
    private EditText userNamer,userPassr,userPass2r,userTelr,userAddressr;
    private Button regBtnr;
    private String str_unamer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initViews();//初始化
        initEvents();//点击事件处理
    }

    private void initViews() {

        userNamer = (EditText) findViewById(R.id.unamer);
        userPassr = (EditText) findViewById(R.id.upassr);
        userTelr = (EditText) findViewById(R.id.utelr);
        userAddressr = (EditText) findViewById(R.id.uadresr);
        userPass2r = (EditText) findViewById(R.id.upass2r);
        regBtnr = (Button) findViewById(R.id.regbtnr);
    }

    private void initEvents() {

        ImageButton imageBack = (ImageButton) findViewById(R.id.image_back);
        imageBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(RegisterActivity.this,LoginActivity.class);
                startActivityForResult(intent,1);
            }
        });

        regBtnr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                str_unamer = userNamer.getText().toString();
                String str_upass = userPassr.getText().toString();
                String str_upass2r = userPass2r.getText().toString();
                String str_tel = userTelr.getText().toString();
                String str_address = userAddressr.getText().toString();
                if (str_unamer.equals("")){
                    Toast.makeText(RegisterActivity.this,"用户名为空",Toast.LENGTH_SHORT).show();
                    return;
                }

                   else  if (str_upass.equals("")){
                    Toast.makeText(RegisterActivity.this,"密码为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                 else if (str_upass2r.equals("")) {
                        Toast.makeText(RegisterActivity.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                        return;
                    } else {
                        UserModel userModel = new UserModel();
                        //1.发送请求
                        Call<UserBean> call = userModel.register
                                (str_unamer,str_upass,str_tel,str_address);
                        //2.处理结果
                        Callback<UserBean> callback = new Callback<UserBean>() {
                            @Override
                            public void onResponse(Call<UserBean> call, Response<UserBean> response) {

                                UserBean user = response.body();
                                if (user.equals("0")) {
                                    Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();

                                }
                            }

                            @Override
                            public void onFailure(Call<UserBean> call, Throwable t) {

                            }
                        };

                        //3.call and callback 捆绑
                        call.enqueue(callback);
                    }

            }
        });
    }
}
